#include "city.h"

#include <assert.h>
#include <stdlib.h>
#include <sqlite3.h>

#include "global.h"

City* new_city(Region* region, char* name, int n)
{
	assert(db);

	City* city = malloc(sizeof(City));
	city->name = name;
	if(strlen(city->name) >= 40)
		city->name[39] = '\0';

	// create information about the city
	int w, h, xi, yi;
	if(n <= 4)
	{
		w = region->w / 4;
		h = region->h / 4;
		xi = (n-1) * (region->w / 4);
		yi = 0;
	}
	else if(n <= 6)
	{
		w = region->w / 2;
		h = region->h / 4;
		xi = (n-5) * (region->w / 2);
		yi = region->h / 4;
	}
	else
	{
		w = region->w;
		h = region->h / 2;
		xi = 0;
		yi = region->h / 2;
	}
	int i=0, xx, yy;
	city->w = w;
	city->h = h;
	city->tile = malloc(sizeof(Tile) * w * h);
	for(xx=xi; xx<w; xx++)
		for(yy=yi; yy<h; yy++)
		{
			uint8_t tile = region->data[xx + (yy * region->w)];
			if(tile == TREES)
			{
				city->tile[i].terrain = LAND;
				city->tile[i].n_trees = (rand() % 2) + 1;
			}
			else
			{
				city->tile[i].terrain = tile;
				city->tile[i].n_trees = 0;
			}
			i++;
		}


	// store city on the DB
	char sql[1024];
	sprintf(sql, "insert into cities ( code, name, w, h ) "
		     "       values ( %d, '%s', %d, %d );", n, name, w, h);

	// TODO - temporary
	char* er;
	sq3_test(sqlite3_exec(db, sql, NULL, NULL, &er));
	if(er)
	{
		fprintf(stderr, "%s\n", er);
		sqlite3_free(er);
	}

	// insert information about tiles in DB
	int x, y;
	sqlite3_stmt* stmt;
	sq3_test(sqlite3_exec(db, "begin;", NULL, NULL, NULL));
	sq3_test(sqlite3_prepare_v2(db, "insert into tiles ( city, x, y, n_trees, terrain, structure ) "
				        " values ( ?, ?, ?, ?, ?, NULL );", -1, &stmt, NULL));
	i = 0;
	for(y=0; y<h; y++)
		for(x=0; x<w; x++)
		{
			sq3_test(sqlite3_reset(stmt));
			sq3_test(sqlite3_bind_int(stmt, 1, n));
			sq3_test(sqlite3_bind_int(stmt, 2, x));
			sq3_test(sqlite3_bind_int(stmt, 3, y));
			sq3_test(sqlite3_bind_int(stmt, 4, city->tile[i].terrain));
			sq3_test(sqlite3_bind_int(stmt, 5, city->tile[i].n_trees));
			sq3_test(sqlite3_step(stmt));
			i++;
		}
	sq3_test(sqlite3_finalize(stmt));
	sq3_test(sqlite3_exec(db, "commit;", NULL, NULL, NULL));

	return city;
}


City* load_city(Region* r, int pos)
{
	assert(db);

	// create object
	City* city = malloc(sizeof(city));

	// load metadata from database
	sqlite3_stmt* stmt;
	sq3_test(sqlite3_prepare_v2(db, 
				"select name, w, h from cities where code = ?",
				-1, &stmt, NULL));
	sq3_test(sqlite3_bind_int(stmt, 1, pos));
	if(sqlite3_step(stmt) != SQLITE_ROW)
		ui_error(_("City not found in database."));
	city->name = strdup((char*)sqlite3_column_text(stmt, 0));
	city->w = sqlite3_column_int(stmt, 1);
	city->h = sqlite3_column_int(stmt, 2);
	sq3_test(sqlite3_finalize(stmt));

	// load tiles
	int status;
	sq3_test(sqlite3_prepare_v2(db, 
				"select x, y, n_trees, terrain from tiles "
				"where city = ?",
				-1, &stmt, NULL));
	sq3_test(sqlite3_bind_int(stmt, 1, pos));
	if(sqlite3_step(stmt) != SQLITE_ROW)
		ui_error(_("City tiles not found in database."));
	city->tile = malloc(sizeof(Tile) * city->w * city->h);
	while((status = sqlite3_step(stmt) == SQLITE_ROW))
	{
		// TODO
	}
	sq3_test(status);
	sq3_test(sqlite3_finalize(stmt));

	return city;
}
